package it.valeandre.omino.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import it.valeandre.omino.R;
import it.valeandre.omino.fragment.MatchFormationPageFragment;
import it.valeandre.omino.utils.Utils;

/**
 * Created by Getta on 27/12/13.
 */
public class FormationView extends LinearLayout {

    private LinearLayout mGoalKeeperContainer;
    private LinearLayout mDefenseContainer;
    private LinearLayout mMidfieldContainer;
    private LinearLayout mForwardContainer;

    private final int DEFAULT_IMAGE_SIZE = 15;

    private int mItemImageResId = 0;

    public FormationView(Context context) {
        super(context);
        init(context, null);
    }

    public FormationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public FormationView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = context.getTheme().obtainStyledAttributes(
                    attrs,
                    R.styleable.FormationView,
                    0, 0);

            try {
                mItemImageResId = a.getResourceId(R.styleable.FormationView_itemResId, 0);
            } finally {
                a.recycle();
            }
        }

        setBackgroundResource(R.drawable.soccer_field_transparant);
        setOrientation(LinearLayout.VERTICAL);
        createLineups();
    }

    private void createLineups() {

        LinearLayout.LayoutParams lineupsParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f);
        mGoalKeeperContainer = new LinearLayout(getContext());
        mDefenseContainer = new LinearLayout(getContext());
        mMidfieldContainer = new LinearLayout(getContext());
        mForwardContainer = new LinearLayout(getContext());

        mGoalKeeperContainer.setLayoutParams(lineupsParams);
        mDefenseContainer.setLayoutParams(lineupsParams);
        mMidfieldContainer.setLayoutParams(lineupsParams);
        mForwardContainer.setLayoutParams(lineupsParams);

        addView(mGoalKeeperContainer);
        addView(mDefenseContainer);
        addView(mMidfieldContainer);
        addView(mForwardContainer);

        setPadding(0, (int)Utils.dipToPixels(getContext(), 10f), 0, 0);
    }

    public void fill(MatchFormationPageFragment.Data data) {

        mGoalKeeperContainer.addView(createItem(data.lineupGoalkeeper.trim(), R.drawable.circle_yellow));

        String[] players = data.lineupDefense.split(";");
        for(int i=0; i<players.length; i++) {
            mDefenseContainer.addView(createItem(players[i].trim(), R.drawable.circle_blue));
        }

        players = data.lineupMidfield.split(";");
        for(int i=0; i<players.length; i++) {
            mMidfieldContainer.addView(createItem(players[i].trim(), R.drawable.circle_green));
        }

        players = data.lineupForward.split(";");
        for(int i=0; i<players.length; i++) {
            mForwardContainer.addView(createItem(players[i].trim(), R.drawable.circle_red));
        }

    }

    private LinearLayout createItem(String name, int itemImageResId) {
        name = name.replace(" ", "\n");
        LinearLayout.LayoutParams itemContainerParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
        LinearLayout itemContainer = new LinearLayout(getContext());

        itemContainer.setLayoutParams(itemContainerParams);
        itemContainer.setOrientation(LinearLayout.VERTICAL);
        itemContainer.setGravity(Gravity.CENTER_HORIZONTAL);

        LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams((int)Utils.dipToPixels(getContext(), DEFAULT_IMAGE_SIZE), (int)Utils.dipToPixels(getContext(), DEFAULT_IMAGE_SIZE));
        ImageView image = new ImageView(getContext());
        image.setLayoutParams(imageParams);
        image.setImageResource(itemImageResId);

        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        TextView text = new TextView(getContext());
        text.setLayoutParams(textParams);
        text.setGravity(Gravity.CENTER_HORIZONTAL);
        text.setText(name);

        text.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        text.setTextColor(getResources().getColor(android.R.color.white));

        itemContainer.addView(image);
        itemContainer.addView(text);

        return itemContainer;
    }
}
